<template>
  <!--毕业设计（论文）成绩评定书-->
  <div class="result-release">
    <div class="edu-page-warp">
      <condition-card @reset="reset" @search="searchList" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="毕业年份">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.graduationYear"
                              @change="handleGradeChange" value-format="yyyy" type="year"
                              placeholder="请选择">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行政学院">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange"
                         class="search-item" clearable>
                <el-option v-for="item in source.studentColleges" :key="item.collegeId"
                           :label="item.collegeIdI18n" :value="item.collegeId"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="专业">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId"
                           :label="item.subjectName" :value="item.subjectId"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">学生列表</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <el-input v-model="queryForm.studentName" placeholder="请输入学号、姓名搜索" style="width:300px"
                      @keyup.enter.native="searchList" clearable>
              <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer"
                 @click="searchList"></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading"
                       element-loading-text="Loading" element-loading-spinner="el-icon-loading"
                       element-loading-background="rgba(255, 255, 255, .48)">打印
            </el-button>
          </div>
        </div>
        <edu-table ref="resultTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column label="毕业年份" prop="graduationYear" min-width="80"/>
          <edu-table-column label="学号" prop="studentId" min-width="90"></edu-table-column>
          <edu-table-column label="姓名" prop="studentName" min-width="100"></edu-table-column>
          <edu-table-column label="行政学院" prop="collegeIdI18n" min-width="100"></edu-table-column>
          <edu-table-column label="专业" prop="subjectIdI18n" min-width="90"></edu-table-column>
          <edu-table-column label="课题名称" prop="thesisTitle" min-width="80"></edu-table-column>
          <edu-table-column label="最终成绩" prop="result" :formatter="resultFormat"
                            min-width="80"></edu-table-column>
        </edu-table>
      </el-card>
    </div>
  </div>
</template>
<script>
  import API from "common/src/api/underGraduationDesign/resultEvaluate.js";
  import tools from 'common/src/views/underGraduationDesign/tools.js';
  export default {
    name: 'designResultRelease',
    data() {
      return {
        api: API,
        getList: '/api/designservice/resultPublish/queryFinalResult',
        queryForm: {
          graduationYear: '',
          collegeId: '',
          subjectId: '',
          activeAuthId: '',
          studentName: ''
        },
        source: {
          studentColleges: [], // 行政学院
          collegeOptions: [], // 学院
          subjectOptions: [] // 专业
        },
        resultSelectedData: [], // 勾选的列表数据
        loading: false,
        resultViewForm: {},
        resultReleaseForm: {},
        resultTypeOptions: [{
          label: '优',
          value: 0
        }, {
          label: '良',
          value: 1
        }, {
          label: '中',
          value: 2
        }, {
          label: '及格',
          value: 3
        }, {
          label: '不及格',
          value: 4
        }]
      };
    },
    mounted() {
      this.queryForm.graduationYear = new Date().getFullYear() + '';
      this.getCollege();
      this.queryForm.activeAuthId = localStorage.getItem("activeAuthId");
      this.searchList();
      tools.initAllCollege(this.source.studentColleges);
    },
    methods: {
      searchList() {
        this.$refs.resultTable.search(this.queryForm);
      },
      reset() {
        this.queryForm = {
          graduationYear: '',
          collegeId: '',
          subjectId: '',
          activeAuthId: '',
          teachingClassCode: ''
        };
        this.source.subjectOptions = [];
        this.queryForm.activeAuthId = localStorage.getItem("activeAuthId");
        this.queryForm.graduationYear = new Date().getFullYear() + '';
        this.searchList();
      },
      handleSelectChange(val) {
        this.resultSelectedData = val;
      },
      // 毕业年份change
      handleGradeChange(val) {
        this.queryForm.subjectId = "";
        this.source.subjectOptions = [];
        if (val && this.queryForm.collegeId) {
          this.handleCollegeChange(this.queryForm.collegeId);
        }
      },
      // 查学院
      getCollege() {
        this.api.getCollege().then(res => {
          this.source.collegeOptions = res;
        });
      },
      // 根据年份学院查专业
      handleCollegeChange(val) {
        this.queryForm.subjectId = "";
        this.source.subjectOptions = [];
        if (val && this.queryForm.graduationYear) {
          const params = {
            grade: this.queryForm.graduationYear,
            college: val
          };
          this.api.getSubject(params).then(res => {
            this.source.subjectOptions = res.data;
          }).catch();
        }
      },
      // 成绩格式化
      resultFormat(row, column, cellValue, index) {
        let resultLabel = cellValue;
        this.resultTypeOptions.forEach(item => {
          if (item.value === cellValue) {
            resultLabel = item.label;
          }
        });
        return resultLabel;
      },
      // 导出
      onExport() {
        const params = {
          condition: this.queryForm,
          selectList: this.resultSelectedData
        };
        this.api.export(params).then(res => {
          if (res.code == 200) {
            // this.delayRequest(res.data.key);
            tools.downloadFile(res.data.key,
              "毕业设计(论文)成绩评定书.zip",
              this.api.getExportStatus, this
            );
          }
        });
      },
      // 查文件导出状态，等待导出
      async delayRequest(key) {
        this.loading = true;
        await new Promise(resolve => setTimeout(resolve, 3000));
        this.api.getExportStatus(key).then(res => {
          this.loading = false;
          if (res.code === 200 && res.data.status && res.data.path) {
            let path = 'api/designservice/resultPublish/downLoadMore?path=' + res.data.path
              + "&fileName=studentResult.zip";
            window.open(path);
          } else {
            this.delayRequest(key);
          }
        }).catch(err => {
          this.loading = false;
          this.delayRequest(key);
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .result-release {
    .search-item {
      width: 100%;
    }
  }
</style>
